package com.ingrid.javi.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DAO extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "dbJavi";
	private static final int CURRENT_VERSION = 1;

	private static final String CREATE_MOVIES = "CREATE TABLE movies("
			+ "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "titulo TEXT,"
			+ "ano INTEGER," + "sinopse TEXT," + "tipo INTEGER,"
			+ "nota INTEGER)";
	private static final String SELECT_MOVIES = "SELECT _id, titulo"
			+ " FROM movies WHERE tipo = 0 ORDER BY titulo";
	private static final String SELECT_WISHES = "SELECT _id, titulo"
			+ " FROM movies WHERE tipo = 1 ORDER BY titulo";
	public static final String COLUMN_TITLE = "titulo";
	private static final String MOVIE_BY_ID = "SELECT * FROM movies WHERE _id = ?";

	public DAO(Context context) {
		super(context, DATABASE_NAME, null, CURRENT_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_MOVIES);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	public Cursor listMovies() {
		return this.getReadableDatabase().rawQuery(SELECT_MOVIES, null);
	}

	public Cursor listWish() {
		return this.getReadableDatabase().rawQuery(SELECT_WISHES, null);
	}

	public void save(Movie movie) {
		ContentValues cv = new ContentValues();
		cv.put("titulo", movie.getTitulo());
		cv.put("ano", movie.getAno());
		cv.put("sinopse", movie.getSinopse());
		cv.put("tipo", movie.getTipo());
		cv.put("nota", movie.getNota());

		this.getWritableDatabase().insert("movies", null, cv);
	}

	public Movie getMovieById(long movieId) {
		Cursor cursor = this.getReadableDatabase().rawQuery(MOVIE_BY_ID,
				new String[] { Long.toString(movieId) });
		cursor.moveToFirst();

		String titulo = cursor.getString(cursor.getColumnIndex("titulo"));
		String ano = cursor.getString(cursor.getColumnIndex("ano"));
		String sinopse = cursor.getString(cursor.getColumnIndex("sinopse"));
		String nota = cursor.getString(cursor.getColumnIndex("nota"));

		return new Movie(titulo, ano, sinopse, nota);
	}

	public Movie getWishMovieId(long wishId) {
		Cursor cursor = this.getReadableDatabase().rawQuery(MOVIE_BY_ID,
				new String[] { Long.toString(wishId) });
		cursor.moveToFirst();

		String titulo = cursor.getString(cursor.getColumnIndex("titulo"));
		String ano = cursor.getString(cursor.getColumnIndex("ano"));
		String sinopse = cursor.getString(cursor.getColumnIndex("sinopse"));

		return new Movie(titulo, ano, sinopse);
	}

	public void markWish(long movieId, String nota) {
		ContentValues cv = new ContentValues();
		cv.put("tipo", Movie.TIPO_FILME);
		cv.put("nota", nota);
		this.getWritableDatabase().update("movies", cv , "_id=?", new String[]{Long.toString(movieId)});
	}

}
